package com.rqbl.controller.check;

import com.sun.mail.util.MailSSLSocketFactory;
import com.sun.org.glassfish.gmbal.ParameterNames;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;
import java.util.Properties;
import java.util.Random;

@Controller
public class SendEmailController {

    String str = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
    Random random = new Random();

    @RequestMapping("/sendEmail")
    @ResponseBody
    public String sendEmail(@RequestParam("email")String email, HttpSession httpSession){
        //创建一个配置文件并保存
        Properties properties = new Properties();

        properties.setProperty("mail.host","smtp.qq.com");

        properties.setProperty("mail.transport.protocol","smtp");

        properties.setProperty("mail.smtp.auth","true");
        Session session;
        Transport transport = null;

        //QQ存在一个特性设置SSL加密
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);


            System.out.println(email);

            //创建一个session对象
            session = Session.getDefaultInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("2376475964@qq.com","immsmzyaktqqeaaf");
                }
            });

            //开启debug模式
            session.setDebug(true);

            //获取连接对象
            transport = session.getTransport();

            //连接服务器
            transport.connect("smtp.qq.com","2376475964@qq.com","immsmzyaktqqeaaf");

            //创建邮件对象
            MimeMessage mimeMessage = new MimeMessage(session);

            //邮件发送人
            mimeMessage.setFrom(new InternetAddress("2376475964@qq.com"));

            //邮件接收人
            mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(email));

            //邮件标题
            mimeMessage.setSubject("热趣部落——验证码");

            //邮件内容
            String code = randomCode();
            httpSession.setAttribute("emailCode",code);
            String text = "您的验证码是："+code+",请在10分钟内使用。";

            mimeMessage.setContent(text,"text/html;charset=UTF-8");

            //发送邮件
            transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());

            //关闭连接
            transport.close();
            return "验证码发送成功";
        } catch (Exception e) {
            throw new RuntimeException(e);

        }
    }

    public String randomCode(){
        String code="";
        for (int i=0;i<4;i++){
            char c=str.charAt(random.nextInt(str.length()));
            code=code+c;
        }
        return code;
    }

}
